# -*- coding:utf-8 -*-
from rest_framework import status
from rest_framework.viewsets import ModelViewSet

from ..filters.projectFilter import ProjectFilter
from ..ser.projectser import ProjectSer, createProjectSer, patchProjectSer, ProjectSelectListSer
from ..models.project import Project
from Apps.common.response import SuccessResponse, ErrorResponse
from Apps.user.models import User


class ProjectView(ModelViewSet):
    serializer_class = ProjectSer
    queryset = Project.objects.all()
    filter_class = ProjectFilter

    # authentication_classes = []

    def list(self, request, *args, **kwargs):
        queryset = Project.objects.filter(is_delete="0")
        queryset = self.filter_queryset(queryset)

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return SuccessResponse(serializer.data)

    def create(self, request, *args, **kwargs):
        request.data["creator"] = request.user.id
        request.data["modifier"] = request.user.id
        serializer = createProjectSer(data=request.data)
        serializer.is_valid(raise_exception=True)

        # serializer.creator = User.objects.get(id=request.user.id)
        # serializer.modifier = User.objects.get(id=request.user.id)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return SuccessResponse(serializer.data, headers=headers)

    def update(self, request, *args, **kwargs):
        request.data["modifier"] = request.user.id
        request.data["creator"] = request.user.id
        partial = kwargs.pop('partial', False)
        instance = self.get_object()
        if instance.is_delete == "1":
            return ErrorResponse(msg="该项目已删除，不能编辑")
        serializer = patchProjectSer(instance, data=request.data, partial=partial)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)

        if getattr(instance, '_prefetched_objects_cache', None):
            # If 'prefetch_related' has been applied to a queryset, we need to
            # forcibly invalidate the prefetch cache on the instance.
            instance._prefetched_objects_cache = {}

        return SuccessResponse(serializer.data)

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        self.perform_destroy(instance)
        return SuccessResponse()

    def perform_destroy(self, instance):
        # 逻辑删除
        instance.is_delete = "1"
        instance.save()


class getProjectSelectListView(ModelViewSet):
    queryset = Project.objects.filter(is_delete="0").all()
    serializer_class = ProjectSelectListSer
    filter_class = ProjectFilter

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        serializer = self.get_serializer(queryset, many=True)
        return SuccessResponse(serializer.data)
